﻿using QBWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using QBWeb.Models.TestFolder;

namespace QBWeb.BLL
{
    /// <summary>
    /// 试卷模块接口类
    /// </summary>
    public class TestPaperBll
    {
        private static readonly QBDbContext db = new QBDbContext();
        private static readonly SetSqlTools setSql = new SetSqlTools();

        #region 试卷模块功能
        /// <summary>
        /// 创建试卷
        /// </summary>
        /// <param name="testName"></param>
        /// <param name="userName"></param>
        /// <param name="cid"></param>
        public static void CreateTest(string testName,string userName,int cid)
        {
            var issue = setSql.SetIssue(0);//0未发布
            db.Issues.Add(issue);
            var testPaperTb = setSql.SetTestPaperTb(testName, userName, issue.Id,cid);
            db.TestPaperTbs.Add(testPaperTb);

            db.SaveChanges();
        }

        /// <summary>
        /// 发布试卷
        /// </summary>
        /// <param name="id"></param>
        /// <param name="notes"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="onTime"></param>
        public static void PutOut(int id,string notes,int[] classes,DateTime startTime,DateTime endTime,int? onTime)
        {
            var issue = onTime == null ? 
                setSql.SetIssue(db.Issues.Find(id), notes, startTime, endTime, 0,1 )
                : setSql.SetIssue(db.Issues.Find(id), notes, startTime, endTime, (int)onTime, 1);
            db.Entry(issue).State = EntityState.Modified;
            for (int i = 0; i < classes.Length; i++)
            {
                try
                {
                    object[] param = { classes[i],id};
                    string sql = "insert into Class_TestPaperTb values(@P0,@P1)";
                    db.Database.ExecuteSqlCommand(sql, param);
                }
                catch (Exception)
                {
                    throw new Exception("保存失败");
                }
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 编辑试卷
        /// </summary>
        /// <param name="testPaperTbId"></param>
        /// <param name="testName"></param>
        /// <param name="creator"></param>
        public static void EditTest(int testPaperTbId,string testName,string creator)
        {
            var test = db.TestPaperTbs.Where(b => b.Id == testPaperTbId);
            if (!test.Any()) throw new Exception("试卷不存在");
            var data = DateTime.Now;
            object[] param = { testName, creator, data, testPaperTbId };
            var sql = "Update TestPaperTbs Set TestName=@P0,Creator=@P1,ModifyTime=@P2 WHERE Id=@P3";
            try { db.Database.ExecuteSqlCommand(sql, param); }
            catch { throw new Exception("保存失败"); }
        }

        /// <summary>
        /// 删除试卷
        /// </summary>
        /// <param name="testPaperTbId"></param>
        public static void DeleteTest(int testPaperTbId)
        {
            var test = db.TestPaperTbs.Where(t => t.Id == testPaperTbId).ToList();
            if (!test.Any()) throw new Exception("试卷不存在");
            object[] vs = { testPaperTbId };
            const string sqlTestDetails = "Delete From TestDetailsTbs WHERE TestPaperTbId=@P0";
            const string sqlTest = "Delete from TestPaperTbs where Id =@P0";
            const string sqlDelete = "Delete from Class_TestPaperTb where IssueId=@P0";
            const string sqlIssues = "Delete From Issues where Id=@P0";
            try
            {
                db.Database.ExecuteSqlCommand(sqlTestDetails, vs);
                db.Database.ExecuteSqlCommand(sqlTest, vs);
                db.Database.ExecuteSqlCommand(sqlDelete, vs);
                db.Database.ExecuteSqlCommand(sqlIssues, vs);
            }
            catch
            {
                throw new Exception("删除试卷失败");
            }
        }
        #endregion
    }
}